//
// Created by baka on 2025/10/14.
//




#include  "beep.h"



#ifdef BEEP_TASK

#include "cmsis_os.h"
#include  "gpio.h"
#include "tim.h"

osThreadId beepTaskHandle;
osMessageQId beepQueueHandle;




void BEEP_TEST(void)
{
    beep_cmd_t beep_cmd = {1,20,5};
    xQueueSend(beepQueueHandle,&beep_cmd, 0);
}


void beepTask(void const * argument);

void beep_task_init(void)
{
    osThreadDef(beepTask, beepTask, osPriorityNormal, 0, 128);
    beepTaskHandle = osThreadCreate(osThread(beepTask), NULL);
    osMessageQDef(beepQueue, 8, beep_cmd_t);
    beepQueueHandle = osMessageCreate(osMessageQ(beepQueue), NULL);
}


void beepTask(void const * argument)
{
    beep_cmd_t beep_cmd;
    for (;;)
    {
        if (xQueueReceive(beepQueueHandle,&beep_cmd,portMAX_DELAY) == pdTRUE)
        {
            for (int i=0;i<beep_cmd.beep_count;i++)
            {
                beep_on();
                osDelay(beep_cmd.on_time);
                beep_off();
                osDelay(beep_cmd.off_time);
            }

        }

    }
}







void beep_on(void)
{
    HAL_TIM_PWM_Start(&htim10, TIM_CHANNEL_1);
}

void beep_off(void)
{
    HAL_TIM_PWM_Stop(&htim10, TIM_CHANNEL_1);
}



//使用参考
void beep_task_test(void)
{
    beep_cmd_t beep_cmd = {
        1,
        20,
        20
    };
    uint8_t i = 1;
    if (i == 1)
    {
        //如果队列满则等待10个ticks
        xQueueSend(beepQueueHandle,&beep_cmd,pdMS_TO_TICKS(10));
    }
}


#endif
